/**
 * @author 李亚松
 * @email  lysong01@gmail.com
 * 
 * DivideArticle.java 2012-10-9
 * description 
 */
package liyasong.chinese;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import liyasong.util.DirectiryUtils;

/*
 * read articles from files, then divide article to sentence!
 */
public class DivideArticles {

	public static int count = 0;
	
	private List<String> filePaths;
	
	private final String delimiter = "。”|。|？”|？";
	private ArrayList<String> sentences;
	
	public DivideArticles(String filePath) {
		this.filePaths = new ArrayList<>();
		this.filePaths.add(filePath);
		sentences = new ArrayList<String>();
	}
	
	public DivideArticles(List<String> filePaths) {
		this.filePaths = filePaths;
		sentences = new ArrayList<String>();
	}

	public ArrayList<String> getSentences() {
		return sentences;
	}
	
	/*
	 * 考虑此类只为中文服务，所以在对文章分句时，过滤掉了非中文开头的句子
	 */
	private void divide(File file) {
		if (file.exists()) {
			try {
				BufferedReader br = new BufferedReader(new FileReader(file));
				String str = "";
				while((str = br.readLine()) != null) {
					if(str.length() != 0) {
						String[] strs = str.split(delimiter);
						for (String string : strs) {
							if(string.length() >= 10 && string.length() <= 100
									&& string.substring(0, 1).matches("[\u4e00-\u9fa5]")) {
								sentences.add(string);
							}
						}	
					}
				}
				br.close();	
			} catch (IOException e) {
				System.out.println(e.getMessage());
			}
		} else {
			System.out.println(file.getName() + "文件不存在！");
		}
	}
	
	public void divide() {
		if(filePaths != null) {
			System.out.println("begin to divide the articles");
			long start = System.currentTimeMillis();
			for (String filePath : filePaths) {
				File file = new File(filePath);
				divide(file);
			}
			System.out.println("divide over. The time costs "+
					(System.currentTimeMillis()-start)/1000+"s.");
		} else {
			System.out.println("make sure that the filePaths is not null");
		}
	}
	
	// TODO 这个函数可能有bug 
	public void resultWriteJson(String outFilePath) {
		File file = new File(outFilePath+".js");
		try {
			BufferedWriter bf = new BufferedWriter(new FileWriter(file));
			bf.write("var DATA = [\n");
			int size = sentences.size();
			for (int i = 0; i < size-1; i++) {
				bf.write("\t{ id:"+i+", label:\""+sentences.get(i)+"\"},\n");
			}
			bf.write("\t{ id:"+(size-1)+", label:\""+sentences.get(size-1)+"\"}\n];\n");
			bf.flush();
			bf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public void resultWrite(String outFilePath, String dirName) {
		char endChar = outFilePath.charAt(outFilePath.length()-1);
		outFilePath = endChar == '/' ? outFilePath : outFilePath+"/";
		File file = new File(outFilePath+dirName+"_style.txt");
		try {
			BufferedWriter bf = new BufferedWriter(new FileWriter(file));
			int size = sentences.size();
			for (int i = 0; i < size-1; i++) {
				bf.write(sentences.get(i)+"\n");
			}
			bf.flush();
			bf.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		if (args.length != 2) {
			System.out.println("输入的参数数目不正确");
		} else {
			String inputPath	= args[0];
			String outputPath	= args[1];
			String osName = System.getProperty("os.name");
			String dirSeparator = osName.startsWith("Win") ? "\\":"/";
			String[] strs = inputPath.split(dirSeparator);
			String dirName = strs[strs.length-1];
			DivideArticles da = new DivideArticles(
					DirectiryUtils.getFilePaths(inputPath));
			da.divide();
			da.resultWrite(outputPath, dirName);
			System.out.println("the articles are divided to " + 
					da.getSentences().size() + " sentences");
		}
	}

}
